package com.xqboss.apps.service.hama;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.xqboss.apps.constant.RedisKey;
import com.xqboss.apps.domain.hama.HamaMixConfig;
import com.xqboss.apps.enums.nft.UserNftStatusEnum;
import com.xqboss.apps.mapper.hama.HamaMixConfigMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.service.user.SysUserNftService;
import com.xqboss.common.core.redis.RedisCache;
import com.xqboss.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 浑天灵池配置 服务实现类
 * </p>
 *
 * @author 木鱼
 * @since 2024-02-28
 */
@Service
@Slf4j
public class HamaMixConfigService extends ServiceImpl<HamaMixConfigMapper, HamaMixConfig> {
    @Autowired
    private SysUserNftService sysUserNftService;
    @Autowired
    private RedisCache redisCache;


    public Long[] getNftLeaveInfo(Long nftId,Long userId,Integer number){
        return this.sysUserNftService.getAppointNumNftNumber(nftId, userId, number);
    }


    /**
     * 销毁使用的藏品
     *
     * @param nftLeaveInfo 藏品numberid集
     * @param userId 用户id
     */
    public void burnNftLeaveInfo(Long[] nftLeaveInfo, Long userId,UserNftStatusEnum statusEnum) {
        this.sysUserNftService.commonDestruction(userId, statusEnum,nftLeaveInfo);
    }

    /**
     * 获取启用的投放装备配置
     *
     * @param isCache true-走缓存
     *
     * @return 列表
     */
    public List<HamaMixConfig> getCacheData(boolean isCache) {
        List<HamaMixConfig> configs = isCache?redisCache.getCacheObject(RedisKey.SPORT_MIX_CONFIG):this.lambdaQuery().list();
        if (CollUtil.isEmpty(configs)) {
            configs = this.lambdaQuery().list();
            redisCache.setCacheObject(RedisKey.SPORT_MIX_CONFIG,configs);
        }
        if (CollUtil.isEmpty(configs)) {
            throw new ServiceException("系统错误，请联系管理员~");
        }
        return configs;
    }

    /**
     * 获取可以投递装备配置<nftId,配置>
     *
     * @return
     */
    public Map<Long, HamaMixConfig> getCacheData(){
        return getCacheData(true).stream().collect(Collectors.toMap(HamaMixConfig::getNftId, Function.identity()));
    }

    /**
     * 获取可以投递指定装备配置
     *
     * @return
     */
    public HamaMixConfig getCacheData(Long nftId){
        Assert.notNull(nftId,()->new ServiceException("装备id不能为空~"));
        return getCacheData().get(nftId);
    }
}